package com.xmlt.ui.editors;

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.apache.catalina.LifecycleException;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.Platform;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.internal.util.BundleUtility;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;

import com.xmlt.ui.server.EmbeddedServer;

/**
 * The activator class controls the plug-in life cycle
 */
public class EditorsPlugin extends AbstractUIPlugin {

	// The plug-in ID
	public static final String PLUGIN_ID = "com.xmlt.ui.editors"; //$NON-NLS-1$

	// The shared instance
	private static EditorsPlugin plugin;

	/**
	 * The constructor
	 */
	public EditorsPlugin() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext
	 * )
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		new Thread(new Runnable() {
			
			@Override
			public void run() {
				try {
					EmbeddedServer.start();
				} catch (LifecycleException e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext
	 * )
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 * 
	 * @return the shared instance
	 */
	public static EditorsPlugin getDefault() {
		return plugin;
	}

	public static String getResourceURL(String imageFilePath) {
		IWorkbench workbench = PlatformUI.isWorkbenchRunning() ? PlatformUI
				.getWorkbench() : null;

		// if the bundle is not ready then there is no image
		Bundle bundle = Platform.getBundle(PLUGIN_ID);
		if (!BundleUtility.isReady(bundle)) {
			return "";
		}
		URL url = bundle.getEntry("/" + imageFilePath);
		URL absUrl = null;
		try {
			absUrl = FileLocator.resolve(url);
		} catch (IOException e) {
			// TODO: add error reporting
			return null;
		}
		String path = absUrl == null ? "" : absUrl.getPath();
		return "file://" + path;
	}
	
	public static File getResourceFile(String resourcePath) {
		IWorkbench workbench = PlatformUI.isWorkbenchRunning() ? PlatformUI
				.getWorkbench() : null;

		// if the bundle is not ready then there is no image
		Bundle bundle = Platform.getBundle(PLUGIN_ID);
		if (!BundleUtility.isReady(bundle)) {
			return null;
		}
		URL url = bundle.getEntry("/" + resourcePath);
		URL absUrl = null;
		try {
			absUrl = FileLocator.resolve(url);
		} catch (IOException e) {
			// TODO: add error reporting
			return null;
		}
		String path = absUrl == null ? "" : absUrl.getPath();
		return new File(path);
	}

}
